/*
 * CinemasForm.java
 *
 * Created on 17 de Junho de 2007, 20:35
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view;

import com.EntertainerMidlet;
import com.ValueObjects.Cinema;
import com.ValueObjects.Filme;
import com.connection.HttpCinemas;
import com.connection.HttpConstants;
import com.connection.HttpFilmes;
import com.connection.HttpSalas;
import com.view.components.CanvasColor;
import com.view.components.CanvasForm;
import com.view.components.CanvasListGroup;
import com.view.components.CanvasListItem;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Gustavo
 */
public class CinemasForm extends CanvasForm implements Runnable{
    
    private Image arrow;
    
    private Image imgTitle;
    
    private int posImgTitle = -200;
    
    private Thread t;
    
    private CanvasListGroup resultList;
    
    private boolean ready = false;
    
    private int gap = 20;
    
    /** Creates a new instance of rankingForm */
    public CinemasForm(EntertainerMidlet midlet, Vector cinemas) {
        super(midlet);
        
        this.midlet = midlet;
        try {
            this.backgroundImg = Image.createImage("/imgs/background.png");
            arrow = Image.createImage("/imgs/Symbol Forward.png");
            imgTitle = Image.createImage("/imgs/busca_title.png");
            
            t = new Thread(this);
            t.start();
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        resultList = new CanvasListGroup(null,0,imgTitle.getHeight(),true);
        resultList.width = getWidth();
        resultList.height = getHeight();
        
        for(int i = 0; i < cinemas.size(); i++) {
            CanvasListItem item = new CanvasListItem(null,"",0,
                    new CanvasColor(0x000000),new CanvasColor(0x000000));
            Cinema cVO = (Cinema) cinemas.elementAt(i);
            item.text = cVO.getNome();
            item.align = Graphics.TOP|Graphics.HCENTER;
            item.id = cVO.getId_cinema();
            resultList.addItem(item);
        }
        
        this.addOkAndBack();
        this.setCommandListener(this);
    }
    
    protected void paint(Graphics g) {
        g.drawImage(this.backgroundImg,0,0,g.TOP|g.LEFT);
        
        g.drawImage(imgTitle,posImgTitle,0,g.TOP|g.LEFT);
        
        ready = true;
        
        if(!resultList.isEmpty()) {
            resultList.g = g;
            resultList.width = getWidth();
            resultList.paint();
        }
    }
    
    protected void keyPressed(int keyCode) {
        String key = getKeyName(keyCode);
        
        if(key.equals("SELECT")) {
            this.commandAction(cmdOk,this);
        }
        
        resultList.keyPressedList(key);
        repaint();
    }
    
    protected void keyReleased(int keyCode) {
        String key = getKeyName(keyCode);
        resultList.keyReleasedList(key);
        repaint();
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        if(cmd == cmdBack) {
            midlet.display.setCurrent(midlet.regiaoForm);
        } else if(cmd == cmdOk) {
            HttpFilmes http = HttpFilmes.getInstance(HttpConstants.BUSCA_FILMES_URL, this);
            Hashtable param = new Hashtable();
            midlet.idCurrentFilme = resultList.selectedItem.id;
            param.put("idCinema",String.valueOf(resultList.selectedItem.id));
            http.doPost(param);
        }
    }
    
    public void run() {
        while(true) {
            if(ready) {
                if(posImgTitle + gap + imgTitle.getWidth() <= getWidth() - 20) {
                    posImgTitle += gap;
                    try {
                        t.sleep(150);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                    repaint();
                } else {
                    t.yield();
                }
            }
        }
    }
    
    protected void initForm() {
        try {
            this.backgroundImg = Image.createImage("/imgs/3d_splat.png");
            arrow = Image.createImage("/imgs/Symbol Forward.png");
            imgTitle = Image.createImage("/imgs/top10_title.png");
            
            t = new Thread(this);
            t.start();
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void goBack() {
    }
    
    public void goAhead() {
    }
    
    public void goFilmes(DataInputStream dataIn, int itNum) {
        t.yield();
        midlet.moviesForm = new SearchResultMovieForm(midlet,Filme.getVectorOfFilmes(dataIn,itNum)) {
            public void goAhead() {
                HttpSalas http = HttpSalas.getInstance(HttpConstants.BUSCA_SALAS_URL,this);              
                midlet.currentMovie = (Filme) movies.elementAt(resultList.selectedIndex);
                Hashtable h = new Hashtable();
                h.put("idCinema",String.valueOf(midlet.idCurrentFilme));
                h.put("idFilme",String.valueOf(resultList.selectedItem.id));
                
                http.doPost(h);
            }
            public void goBack() {
                midlet.display.setCurrent(midlet.cinemaForm);
            }
        };
        midlet.display.setCurrent(midlet.moviesForm);
    }
}
